package nc.bs.pf.changedir;

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.itf.medpub.pub.dd.IMedDefQuote;
import nc.vo.gsp.pub.review.GspReviewItemVO;
import nc.vo.gsp.pub.review.GspReviewVO;
import nc.vo.jcom.lang.StringUtil;
import nc.vo.medpub.bd.pub.MedpubBDConst;
import nc.vo.medpub.pub.util.ObjectUtil;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.lang.UFDouble;

public class CHG4CTOHFH0_after implements nc.vo.pf.change.IchangeVO {

	public AggregatedValueObject retChangeBusiVO(AggregatedValueObject preVo,
			AggregatedValueObject nowVo) throws BusinessException {

		CircularlyAccessibleValueObject srcHeadVO = preVo.getParentVO();
		if(srcHeadVO == null){
			return nowVo;
		}
		CircularlyAccessibleValueObject[] srcItemVOs = preVo.getChildrenVO();
		if (srcItemVOs == null || srcItemVOs.length == 0) {
			return nowVo;
		}
		String reviewNumField = getReviewNumField();
		if (StringUtil.isEmptyWithTrim(reviewNumField)) {
			return nowVo;
		}
		GspReviewVO rsBillVO = (GspReviewVO) nowVo;
		GspReviewItemVO[] rsItemVOs = (GspReviewItemVO[]) rsBillVO
				.getChildrenVO();
		UFDouble cksl = null;
		UFDouble fhsl = null;
		for (int i = 0; i < srcItemVOs.length; i++) {
			cksl = ObjectUtil.toUFDouble2(srcItemVOs[i]
					.getAttributeValue("noutnum"));
			fhsl = ObjectUtil.toUFDouble2(srcItemVOs[i]
					.getAttributeValue(reviewNumField));
			rsItemVOs[i].setNoutnum(cksl.sub(fhsl));
		}
		return rsBillVO;
	}

	public AggregatedValueObject[] retChangeBusiVOs(
			AggregatedValueObject[] preVos, AggregatedValueObject[] nowVos)
			throws BusinessException {

		if (preVos == null || preVos.length == 0 || nowVos == null
				|| nowVos.length == 0) {
			return nowVos;
		}
		for (int i = 0; i < preVos.length; i++) {
			this.retChangeBusiVO(preVos[i], nowVos[i]);
		}
		return nowVos;
	}

	private String getReviewNumField() {
		String defField = null;
		try {
			IMedDefQuote defQuote = (IMedDefQuote) NCLocator.getInstance()
					.lookup(IMedDefQuote.class.getName());
			defField = defQuote.readBillDefQuote("scm_icbill_body", MedpubBDConst.DEFCODE_YFHSL);
		} catch (BusinessException e) {
			Logger.error(e.getMessage(), e);
		}
		return defField;
	}

}
